<?php

defined('SYSPATH') or die('Access error!');

class Model_News extends Model
{

    public $rules = array(
        'name' => array
            (
            'not_empty' => null,
        )
    );

    /**
     *  插入附件
     * @param type $data
     * @return type
     */
    public function add_attachment($data)
    {
        return Model_Attachment::factory('attachment')->insert($data);
    }

    /**
     *  获取新闻列表后台常用
     * @param type $where_arr
     * @return type
     */
    public function get_news_list($where_arr = '', $limits_nums = 10)
    {

        $where = " WHERE 1=1";

        if (Arr::validate_arr($where_arr))
        {
            if (isset($where_arr['category_id']))
            {
                $where .= ' AND n.category_id=' . $where_arr['category_id'];
            }
            if (isset($where_arr['name']))
            {
                $where .= " AND n.name LIKE '%" . $where_arr['name'] . "%'";
            }
        }
        $where .= ' ORDER BY n.id DESC';

        $sql = ' SELECT n.id,n.name,n.add_time,n.position,c.name as category_name ' .
                " FROM " . $this->table . " AS n " .
                " LEFT JOIN " . $this->db->get_table('category') . " AS c ON c.id = n.category_id " . $where;
        $data = $this->get_page_list($sql, $limits_nums);
        return count($data) > 0 ? $data : array();
    }

    /**
     *  获取当前分类及子分类的新闻
     * @param type $category_id
     * @param type $limits_nums
     * @return type
     */
    public function get_allnews_list_by_category_id($category_id, $limits_nums = 10)
    {
        $category_id = intval($category_id);
        if (empty($category_id))
        {
            return array();
        }
        $all_children_category = Model::factory('category')->get_all_children($category_id);
        $children_in = $this->get_in($all_children_category);
        $sql = "SELECT name,color,id,title,keywords,description,add_time,clicks " .
               "FROM " . $this->table . " WHERE category_id " . $children_in . " ORDER BY id DESC";
        $news_list_temp = $this->get_page_list($sql, $limits_nums);
        $news_list = array();
        if (Arr::validate_arr($news_list_temp['list']))
        {
            foreach ($news_list_temp['list'] as $key => $value)
            {
                $color_name = $value['name'];
                if (!empty($value['color']))
                {
                    $color_name = "<font color='" . $value['color'] . "'>" . $color_name . "</font>";
                }
                $value['color_name'] = $color_name;

                if ($value['title'] == '')
                {
                    $value['title'] = $value['name'];
                }
                $value['url'] = $this->create_news_detail_url($value['id']);
                $value['attacment'] = Model::factory('news_attachment')->get_news_attachment_first($value['id']);
                $news_list[] = $value;
            }
        }
        $data['list'] = $news_list;
        $data['page'] = $news_list_temp['page'];
        unset($news_list_temp);
        return $data;
    }

 

    /**
     *  生成新闻详细页面的url
     * @param type $id
     * @return type
     */
    public function create_news_detail_url($id)
    {
        return URL::site('study/detail/' . $id);
    }

    /**
     *  根据文章的id获取某篇文章的详细内容
     * @param type $id
     * @return type
     */
    public function get_detail_by_id($id)
    {

        $temp = $this->get_row(array('id' => $id), 'id,name,title,keywords,description,content,add_time,clicks,source,author,category_id');
        if (Arr::validate_arr($temp))
        {
            $this->update(array('clicks' => $temp['clicks'] + 1), array('id' => $id));
        }
        return $temp;
    }

    /**
     *  获取某个分类下的文章的前一篇
     * @param type $id
     * @param type $category_id
     * @return boolean
     */
    public function get_pre_news($id, $category_id)
    {
        $sql = "SELECT max(id) FROM " . $this->table . " WHERE id < " . $id . " AND category_id=" . $category_id . ' LIMIT 1';
        $pre_news_id = $this->db->get_var($sql);
        if (!empty($pre_news_id))
        {
            $data = $this->get_row(array('id' => $pre_news_id), 'id,name');
            $data['url'] = $this->create_news_detail_url($pre_news_id);
            return $data;
        }
        return array();
    }

    /**
     * 获取某个分类下的文章的下一篇
     * @param type $id
     * @param type $category_id
     * @return boolean
     */
    public function get_next_news($id, $category_id)
    {
        $sql = "SELECT max(id) FROM " . $this->table . " WHERE id > " . $id . " AND category_id=" . $category_id . ' LIMIT 1';
        $next_news_id = $this->db->get_var($sql);
        if (!empty($next_news_id))
        {
            $data = $this->get_row(array('id' => $next_news_id), 'id,name');
            $data['url'] = $this->create_news_detail_url($next_news_id);
            return $data;
        }
        return array();
    }

}